package com.budwk.app.sqfwV2.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.annotation.ApiIdempotent;
import com.budwk.app.sqfw.models.Sqfw_completeflow;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.models.Sqfw_servicerecord;
import com.budwk.app.sqfw.services.SqfwCompleteflowService;
import com.budwk.app.sqfw.vo.GenerateExcelUrlVO;
import com.budwk.app.sqfwDoc.dto.TemplateCodeUpdateDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownExcelLedgerReqVo;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownFileTypesResVo;
import com.budwk.app.sqfwV2.dto.vo.SummaryQueryReqVo;
import com.budwk.app.sqfwV2.service.SummarySafetyService;
import com.budwk.app.sys.services.SysDictService;
import com.budwk.app.sys.vo.SysDictVO;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;

import java.util.List;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-03-11
 *
 * 安全排查
 */
@IocBean
@At("/platform/sqfwV2/form/summary")
@ApiDefinition(tag = "summary表接口实现类")
@Slf4j
public class SummarySafetyController {

    @Inject
    private SummarySafetyService summarySafetyService;
    @Inject
    private SysDictService dictService;
    @Inject
    private SqfwCompleteflowService completeflowService;

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "查询当前登陆人是否创建任务")
    @SaCheckLogin
    public Result<?> queryCurrentTask(@Param("templateFlag") String templateFlag, @Param("type") String type) {
        Sqfw_servicerecord sqfwServicerecord = summarySafetyService.queryCurrentTask(templateFlag, type);
        if (sqfwServicerecord != null) {
            return Result.data(sqfwServicerecord);
        }
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "提交数据/生成PDF")
    @ApiFormParams(implementation = TemplateCodeUpdateDTO.class)
    @SLog(tag = "新增接口", value = "${args[0]}")
    @SaCheckLogin
    @ApiIdempotent
    public Result<?> submitData(@Param("..") TemplateCodeUpdateDTO dto) {
        log.info("summarySafety submitData param: {}", JSONObject.toJSONString(dto));
        return Result.data(summarySafetyService.submitData(dto));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改历史记录/重新生成PDF")
    @ApiFormParams(implementation = TemplateCodeUpdateDTO.class)
    @SLog(tag = "修改历史记录", value = "${args[0]}")
    @SaCheckLogin
    @ApiIdempotent
    public Result<?> updateSummaryData(@Param("..") TemplateCodeUpdateDTO dto) {
        log.info("summarySafety updateSummaryData param: {}", JSONObject.toJSONString(dto));
        return Result.data(summarySafetyService.updateSummaryData(dto));
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "重新生成PDF")
    @ApiImplicitParams(@ApiImplicitParam(name = "sqfwFlowId", description = "工作流id", required = true, check = true))
    @SaCheckLogin
    public Result<?> reGeneratePDF(@Param("sqfwFlowId") String sqfwFlowId, @Param("source") String source) {
        log.info("summarySafety reGeneratePDF sqfwFlowId: {}, source: {}", sqfwFlowId, source);
        return summarySafetyService.reGeneratePDF(sqfwFlowId, source);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询summary表数据")
    @ApiFormParams(implementation = SummaryQueryReqVo.class)
    @SaCheckLogin
    public Result<?> listSummaryByPage(@Param("..") SummaryQueryReqVo dto) {
        log.info("summarySafety listSummaryByPage param: {}", JSONObject.toJSONString(dto));
        //防止前端注入sql
        dto.setSqlExpressionGroup(null);
        return summarySafetyService.listSummaryByPage(dto);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取summary表详情")
    @ApiImplicitParams(@ApiImplicitParam(name = "summaryId", description = "summary表id", required = true, check = true))
    @SaCheckLogin
    public Result<Sqfw_formSummary_new> getSummaryDetail(@Param("summaryId") String summaryId) {
        log.info("summarySafety getSummaryDetail summaryId: {}", summaryId);
        return summarySafetyService.getSummaryDetail(summaryId);
    }

    @At
    @Ok("json")
    @GET
    @ApiImplicitParams(@ApiImplicitParam(name = "completeId", description = "工作流id", required = true, check = true))
    @ApiOperation(name = "查询pdf文档")
    @SaCheckLogin
    public Result<Sqfw_completeflow> getCompleteDetail(@Param("completeId") String completeId) {
        log.info("summarySafety getCompleteDetail completeId: {}", completeId);
        Sqfw_completeflow completeflow = summarySafetyService.getCompleteDetail(completeId);
        return Result.data(completeflow);
    }

    @At
    @Ok("json")
    @DELETE
    @ApiOperation(name = "删除表数据")
    @ApiImplicitParams(@ApiImplicitParam(name = "summaryId", description = "summary表id", required = true, check = true))
    @SaCheckLogin
    public Result<?> deleteData(@Param("summaryId") String summaryId) {
        log.info("summarySafety deleteData summaryId: {}", summaryId);
        summarySafetyService.deleteData(summaryId);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "下载文件(按照编码下载)")
    @ApiFormParams(implementation = DownExcelLedgerReqVo.class)
    @ApiResponses(implementation = GenerateExcelUrlVO.class)
    @SaCheckLogin
    public Result<?> downloadFile(@Param("..") DownExcelLedgerReqVo reqVo) {
        log.info("summarySafety downExcelLedger reqVo: {}", JSONObject.toJSONString(reqVo));
//        reqVo.setAgainGenerate(true);
        return summarySafetyService.downloadFile(reqVo);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "根据输入的查询条件和内容查询列表信息")
    @ApiResponses(implementation = Sqfw_formSummary_new.class)
    @SaCheckLogin
    public Result<?> listByConditionKey(@Param("fillType") String fillType, @Param("address") String address, @Param("placeName") String placeName) {
        log.info("summarySafety listByConditionKey address: {}, placeName: {}", address, placeName);
        return summarySafetyService.listByConditionKey(fillType, address, placeName);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "生成材料列表")
    @ApiResponses(implementation = DownFileTypesResVo.class)
    @SaCheckLogin
    public Result<Pagination> generateFile(@Param("..") SummaryQueryReqVo dto) {
        log.info("summarySafety generateFile start param: {}", JSONObject.toJSONString(dto));
        dto.setSqlExpressionGroup(null);
        return summarySafetyService.generateFile(dto);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "生成材料详情（下载页）")
    @ApiResponses(implementation = DownFileTypesResVo.class)
    @SaCheckLogin
    public Result<DownFileTypesResVo> generateDetailFile(@Param("..") SummaryQueryReqVo dto) {
        log.info("generateDetailFile generateFile start param: {}", JSONObject.toJSONString(dto));
        dto.setSqlExpressionGroup(null);
        return summarySafetyService.generateDetailFile(dto);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "下载模板")
    @ApiResponses(implementation = DownExcelLedgerReqVo.class)
    @SaCheckLogin
    public Result<DownFileTypesResVo> downTemplate(@Param("..") DownExcelLedgerReqVo reqVo) {
        log.info("generateDetailFile downTemplate start param: {}", JSONObject.toJSONString(reqVo));
        reqVo.setDownTemplate(true).setAgainGenerate(true);
        return summarySafetyService.downTemplate(reqVo);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "数据字典")
    @ApiResponses(implementation = SysDictVO.class)
    @SaCheckLogin
    public Result<List<SysDictVO>> listDicts(@Param("dictCode") String dictCode) {
        log.info("summarySafetyService listDicts param: {}", dictCode);
        return Result.data(dictService.listDictByCode(dictCode));
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "查询当前用户的所有任务")
    @ApiResponses(implementation = SummaryQueryReqVo.class)
    @SaCheckLogin
    public Result<?> listCurrentTask(@Param("..") SummaryQueryReqVo reqVo) {
        return Result.data(summarySafetyService.listCurrentTask(reqVo));
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "查询当前商铺最新的填报数据（必须是同一单位下）")
    @ApiResponses(implementation = Sqfw_formSummary_new.class)
    @SaCheckLogin
    public Result<Sqfw_formSummary_new> queryCurrentShopLastestData(@Param("shopId") String shopId, @Param("type") String type) {
        log.info("summarySafetyService queryCurrentShopLastestData start param: {}, type: {}", shopId, type);
        return summarySafetyService.queryCurrentShopLastestData(shopId, type);
    }
}
